﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace ChangDiTools.Demo {
    public class NetSelection {
        [CommandMethod("TestGetSelect")]
        public static void TestGetSelect()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Circle c1=new Circle(Point3d.Origin, Vector3d.ZAxis, 10);
            Circle c2=new Circle(Point3d.Origin, Vector3d.ZAxis, 20);
            Circle c3=new Circle(Point3d.Origin, Vector3d.ZAxis, 30);
            db.AddEntity(c1, c2, c3);
            PromptSelectionResult psr = ed.GetSelection();
            if (psr.Status!=PromptStatus.OK)
            {
                return;
            }

            SelectionSet ss = psr.Value;
            AcAp.ShowAlertDialog($"选择集中实体数量：{ss.Count}");
        }

        [CommandMethod("MergeSelection")]
        public static void MergeSelection() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptSelectionResult ss1 = ed.GetSelection();
            if(ss1.Status!=PromptStatus.OK) return;
            AcAp.ShowAlertDialog($"第一个选择集中实体的数量:{ss1.Value.Count}");
            var ss2 = ed.GetSelection();
            if (ss2.Status != PromptStatus.OK) return;
            AcAp.ShowAlertDialog($"第二个选择集中实体的数量:{ss2.Value.Count}");
            var ss3 = ss1.Value.GetObjectIds().Union(ss2.Value.GetObjectIds());
            AcAp.ShowAlertDialog($"合并后选择集中实体的数量:{ss3.Count()}");
        }

        [CommandMethod("DelFromSelection")]
        public static void Test() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptSelectionResult ss1 = ed.GetSelection();
            if(ss1.Status!=PromptStatus.OK) return;
            AcAp.ShowAlertDialog($"第一个选择集中实体的数量：{ss1.Value.Count}");
            var ss2 = ed.GetSelection();
            if (ss2.Status != PromptStatus.OK) return;
            AcAp.ShowAlertDialog($"第二个选择集中实体的数量:{ss2.Value.Count}");
            var ss3 = ss1.Value.GetObjectIds().Except(ss2.Value.GetObjectIds());
            AcAp.ShowAlertDialog($"删除第二个选择集后第一个选择集中实体的数量:{ss3.Count()}");
        }

        [CommandMethod("TestPickFirst",CommandFlags.UsePickSet)]
        public static void TestPickFirst() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            PromptSelectionResult psr = ed.SelectImplied();
            if (psr.Status==PromptStatus.OK)
            {
                SelectionSet ss1 = psr.Value;
                AcAp.ShowAlertDialog($"PickFirst示例：当前已选择的实体个数：{ss1.Count}");
                ed.SetImpliedSelection(new ObjectId[0]);
                psr = ed.GetSelection();
                if (psr.Status==PromptStatus.OK)
                {
                    ed.SetImpliedSelection(psr.Value.GetObjectIds());
                    SelectionSet ss2 = psr.Value;
                    AcAp.ShowAlertDialog($"PickFirst示例：当前已选择的实体个数：{ss2.Count}");
                }
            }
            else
            {
                AcAp.ShowAlertDialog($"PickFirst示例：当前已选择的实体个数：0");
            }
        }

        [CommandMethod("TestPolygonSelect")]
        public static void TestPolygonSelect() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Point3dList pts = new Point3dList();
            PromptEntityResult per = ed.GetEntity("请选择多段线");
            if (per.Status != PromptStatus.OK) return;
            using (var tr = doc.TransactionManager.StartTransaction()) {
                Polyline pl = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Polyline;
                if (pl != null) {
                    for (int i = 0; i < pl.NumberOfVertices; i++) {
                        Point3d point = pl.GetPoint3dAt(i);
                        pts.Add(point);
                    }

                    PromptSelectionResult psr = ed.SelectWindowPolygon(pts);
                    if (psr.Status == PromptStatus.OK)
                        AcAp.ShowAlertDialog($"选择集中实体的数量：{psr.Value.Count}");
                }
                tr.Commit();
            }
        }

        [CommandMethod("TestSelectException")]
        public static void TestSelectException() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Point3d pt1 = Point3d.Origin;
            Point3d pt2=new Point3d(100,100,0);
            PromptSelectionResult psr = ed.SelectCrossingWindow(pt1, pt2);
            if (psr.Status == PromptStatus.OK)
                AcAp.ShowAlertDialog($"选择集中实体的数量：{psr.Value.Count}");
        }

        [CommandMethod("TestFilter")]
        public static void TestFilter() {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            TypedValueList tvs=new TypedValueList();
            tvs.Add(DxfCode.LayerName,"ZC-XL-CD");
            tvs.Add(typeof(Circle));
            SelectionFilter filter=new SelectionFilter(tvs);
            PromptSelectionResult psr = ed.SelectAll(filter);
            if(psr.Status==PromptStatus.OK)
                AcAp.ShowAlertDialog($"选择集中实体的数量：{psr.Value.Count}");
        }
    }
}
